import java.sql.ResultSet;
import java.util.Scanner;


public class ConnectionList {
	private int connectionfactor;
	private String[] hosts;
	private int[] ports;
	private long current;
	
	public ConnectionList(){
		hosts = new String[100];
		ports = new int[100];
		connectionfactor = 0;
		current = 0;
	}
	
	public ConnectionList(ResultSet rs, int connectionfactor){
		this.connectionfactor = connectionfactor;
		this.hosts = new String[100];
		this.ports = new int[100];
		int count = 0;
		String host;
		int port;
		int probability;
		String NodeID;
		Scanner parser;
		current = 0;
		//System.out.println("rows");
		try{
			while (rs.next()){
				//System.out.println("Processing row");
				NodeID = rs.getString("NODE2_NAME");
				parser = new Scanner(NodeID);
				parser.useDelimiter(":");
				host = parser.next();
				port = parser.nextInt();
				probability = rs.getInt("CONN_PROBABILITY");
				for (int i = 0; i<100; i++){
					if (i<(count+probability)){
						hosts[i] = host;
						ports[i] = port;
					}
				}
				//System.out.println(host + " - " + port);
				count = count + probability;
				//System.out.println(rs.getString("NODE2_NAME") + " " + rs.getInt("CONN_PROBABILITY"));
			}
		} catch (Exception e){
			System.out.println("ResultSet Error");
			System.out.println(e.getMessage());
		}
		
	}

	public ConnectionList(ResultSet rs){
		connectionfactor = 100;
		this.hosts = new String[100];
		this.ports = new int[100];
		int count = 0;
		String host;
		int port;
		int probability;
		String NodeID;
		Scanner parser;
		current = 0;
		//System.out.println("rows");
		try{
			while (rs.next()){
				//System.out.println("Processing row");
				NodeID = rs.getString("NODE2_NAME");
				parser = new Scanner(NodeID);
				parser.useDelimiter(":");
				host = parser.next();
				port = parser.nextInt();
				probability = rs.getInt("CONN_PROBABILITY");
				for (int i = 0; i<100; i++){
					if (i<(count+probability)){
						hosts[i] = host;
						ports[i] = port;
					}
				}
				//System.out.println(host + " - " + port);
				count = count + probability;
				//System.out.println(rs.getString("NODE2_NAME") + " " + rs.getInt("CONN_PROBABILITY"));
			}
		} catch (Exception e){
			System.out.println("ResultSet Error");
			System.out.println(e.getMessage());
		}
		
	}
	
	public ConnectionList(String host, int port, int connectionfactor){
		this.connectionfactor = connectionfactor;
		this.hosts = new String[100];
		this.ports = new int[100];
		current = 0;
		for (int i = 0; i <100; i++){
			hosts[i] = host;
			ports[i] = port;
		}

	}
	
	public ConnectionList(String host, int port){
		connectionfactor = 100;
		this.hosts = new String[100];
		this.ports = new int[100];
		current = 0;
		for (int i = 0; i <100; i++){
			hosts[i] = host;
			ports[i] = port;
		}

	}
	
	public boolean chooseNextConnection(){
		current = Math.round(Math.random() * 99);
		return ((Math.round(Math.random() * 99))< connectionfactor);
	}
	
	public String getHostName(){
		return hosts[(int)current];
	}
	
	public int getPort(){
		return ports[(int)current];
	}
	
}
